#!coding=utf-8
from appium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
import random

ANDROID_VERSION='4.4.2'
DEVICE='127.0.0.1:62001'


def swipeUp(driver,point_1,point_2):
    #定义屏幕向上滑动操作
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    #print u"屏幕分辨率为:", x, y
    l = (x,y)
    x1 = int(l[0]*0.5)
    y1 = int(l[1]*point_1)
    y2 = int(l[1]*point_2)
    driver.swipe(x1,y1,x1,y2,500)

def swipeLift(driver,point_1,point_2):
    #定义屏幕向上滑动操作
    x = driver.get_window_size()['width']
    y = driver.get_window_size()['height']
    #print u"屏幕分辨率为:", x, y
    l = (x,y)
    y1 = int(145)
    x1 = int(l[0]*point_1)
    x2 = int(l[0]*point_2)
    driver.swipe(x1,y1,x2,y1,500)

def WaitforElementByContentDesc(dr,desc,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_element_by_accessibility_id(desc)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            return element

def WaitforElementsByContentDesc(dr,desc,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_elements_by_accessibility_id(desc)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            if len(element)==0:
                time.sleep(1)
                waittime=waittime-1
                if waittime==0:
                    raise NoSuchElementException
                continue
            else:
                return element

def WaitforElementByName(dr,name,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_element_by_name(name)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            return element

def WaitforElementById(dr,id,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_element_by_id(id)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            return element

def WaitforElementByClass(dr,class_name,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_element_by_class_name(class_name)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            return element

def WaitforElementsByClass(dr,class_name,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_elements_by_class_name(class_name)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            if len(element)==0:
                time.sleep(1)
                waittime=waittime-1
                if waittime==0:
                    raise NoSuchElementException
                continue
            else:
                return element

def WaitforElementsById(dr,id,waittime):
    element=''
    while waittime:
        try:
            element=dr.find_elements_by_id(id)
        except NoSuchElementException:
            time.sleep(1)
            waittime=waittime-1
            if waittime==0:
                raise NoSuchElementException
            continue
        else:
            if len(element)==0:
                time.sleep(1)
                waittime=waittime-1
                if waittime==0:
                    raise NoSuchElementException
                continue
            else:
                return element

def target_click(wd,x1,y1):  #x1,y1为你编写脚本时适用设备的实际坐标
    #print x1,y1
    x_1=x1/720         #计算坐标在横坐标上的比例，其中375为iphone6s的宽
    y_1=y1/1280         #计算坐标在纵坐标667为iphone6s的高
    x = wd.get_window_size()['width']  #获取设备的屏幕宽度
    y = wd.get_window_size()['height'] #获取设备屏幕的高度
    print x_1*x,y_1*y    #打印出点击的坐标点
    wd.tap([(x_1*x, y_1*y)], 500)  #模拟单手点击操作

